package hotel.quartz.oversea.job.switchs;

import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.quartz.UrlSwitchsEnums;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.JacksonUtil;
import hotel.base.oversea.utils.Result;
import hotel.quartz.oversea.config.service.RedisService;
import hotel.quartz.oversea.job.base.BaseJob;
import hotel.quartz.oversea.util.JobRestTemplateUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: lmf
 * @Date: 2021/04/23/14:04
 * @Description:
 * 效验价格房态定时任务 如果发现携程那边是开房美团满房，会单独放在队列里
 * 重新进行推送
 */
@Slf4j
public class SwitchPriceAndStatusRefeshPushJob implements BaseJob {
    private String timestamp = "Spasrpj"+String.valueOf(System.currentTimeMillis());
    @Autowired
    RedisService redisService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RestTemplate restTemplate;
    /***
    * @Author: lmf
    * @Date: 2021/4/23
    */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("=========switch price and status refresh push job 针对异常房态的定时任务开始{},{}",timestamp, DateUtil.getCurrTime());
        long size=  redisTemplate.opsForList().size(CommonConstants.SWITCH_CHECK_HOTEL_PRICE_AND_STATUS_REFRESH_PUSH);
        if(size==0){
            log.info("=========switch price and status refresh push job 队列长度为0 跳过,{},{}",timestamp, DateUtil.getCurrTime());
        }else{
           request();
        }
    }
    /***
    * @Author: lmf
    * @Date: 2021/4/23
     * 发起请求
    */
    private void request() {
        String callbackHost = CommonUrlConstants.SWITCHS_ONLINE_HOST;
        String switchPriceAndtatusRefreshPushJobUrl = callbackHost+ UrlSwitchsEnums.goodsPrice.SWITCH_PRICE_AND_STATUS_REFRESH_PUSH_JOB.getValue();
        Result<Object> response= JobRestTemplateUtils.get(switchPriceAndtatusRefreshPushJobUrl,null,restTemplate);
        log.info("=========switch price and status refresh push job 返回的结果为{}", JacksonUtil.objectToJson(response));
    }
}
